package com.hyperion.blog.controller;

import com.hyperion.blog.common.aop.LogAnnotation;
import com.hyperion.blog.service.CategoryService;
import com.hyperion.blog.vo.ErrorCode;
import com.hyperion.blog.vo.result.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Hyperion
 */
@RestController
@RequestMapping("/categorys")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping
    @LogAnnotation(clazz = "CategoryController", method = "categorys")
    public Result categorys() {
        return Result.success(categoryService.findAll());
    }

    @GetMapping("/detail")
    @LogAnnotation(clazz = "CategoryController", method = "categorysDetail")
    public Result categorysDetail() {
        return Result.success(categoryService.findAllDetail());
    }

    @GetMapping("/detail/{id}")
    @LogAnnotation(clazz = "CategoryController", method = "categoryDetail")
    public Result categoryDetail(@PathVariable("id") Long categoryId) {
        Object data = categoryService.findAllDetailById(categoryId);
        if (data == null) {
            return Result.fail(ErrorCode.PARAMS_ERROR);
        } else {
            return Result.success(data);
        }
    }
}
